﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Sunboxsoft.WinForm;
using WebRef = Sunboxsoft.ASMS.Client.Domain.WebRef;
using Service = Sunboxsoft.ASMS.Client.Domain.Service;
using Sunboxsoft.Common;
using Sunboxsoft.ASMS.Client.UI.Properties;
namespace Sunboxsoft.ASMS.Client.UI.StockWarning
{
    /// <summary>
    /// 库存预警
    /// </summary>
    public partial class StockWarningListView : UserControl, IView
    {
        Promise _promise = new Promise();
        WebRef.VMaterial itemMaterial = null;
        private int _pageIndex;
        private int _pageCount;

        public StockWarningListView()
        {
            InitializeComponent();
            this.ctlGridData.EmbeddedNavigator.ButtonClick += new DevExpress.XtraEditors.NavigatorButtonClickEventHandler(OnEmbeddedNavigatorButtonClick);
        }

        #region IView 成员

        public ViewHeader GetHeader()
        {
            return new ViewHeader("库存预警");
        }
        public void RegisterAction(ActionNodeCollection collection)
        {
            collection
                .AddAction(new ActionNode("view", Resources.isView, "查看", OnViewClick))
                .AddAction(new ActionNode("set", Resources.isSettings, "设置预警值", OnSettingClick))
               .AddAction(new ActionNode("edit", Resources.isEdit, "修改预警值", OnEditClick))
               .AddAction(new ActionNode("refresh", Resources.isRefresh, "刷新", OnRefreshActionClick));
        }
        public void Active(ViewActiveArgs args)
        {
            _promise.Then(() =>
            {
                SearchLookUpEditUtil.BindEWarehouse(this.ctlSearchLookUpWareHouse, _promise, null, true);
            })
                .Then(RefreshData)
                .Run();
        }
        public void Resume(ViewResumeArgs args)
        {
            if (args.ClosedArgs.Result)
            {
                if (args.ClosedArgs.View is Pickers.MaterialPicker)
                {
                    itemMaterial = args.ClosedArgs.Data as WebRef.VMaterial;
                    if (itemMaterial != null)
                    {
                        this.ctlBtnSelectMaterial.Text = itemMaterial.Name;
                    }
                }
                if (args.ClosedArgs.View is StockWarning.SetStockWarningView
                    || args.ClosedArgs.View is Edit.UpdateStockWarningView)
                {
                    RefreshData();
                }
            }
        }
        #endregion

        #region Method
        private int GetPageLimit()
        {
            return 25;
        }
        /// <summary>
        /// 数据刷新
        /// </summary>
        private void RefreshData()
        {
            Guid? materialId = null, wareHouseId = null;
            if (itemMaterial != null)
            {
                materialId = itemMaterial.Id;
            }

            WebRef.VWarehouse itemWareHouse = SearchLookUpEditUtil.GetSelectedValue<WebRef.VWarehouse>(this.ctlSearchLookUpWareHouse);
            if (itemWareHouse != null)
            {
                wareHouseId = itemWareHouse.Id;
            }
            this.ShowMask("loading...");
            UIContext.Current.InvokeAsync(() =>
            {
                PagedResult<WebRef.StockWarning> result = new Service.QueryService().QueryStockWarningPagedList(
                    ModuleContext.Current.BusinessId.Value,
                    ModuleContext.Current.StationId.Value,
                    wareHouseId,
                    materialId,
                    _pageIndex * GetPageLimit(), GetPageLimit());
                this.BeginInvoke(new Action(() =>
                {
                    if (result.IsSuccess)
                    {
                        this.ctlGridData.Paginate<WebRef.StockWarning>(result, out _pageIndex, out _pageCount);
                        _promise.Resolve();
                    }
                    else
                    {
                        UIContext.Current.ViewManager.ShowError(result.Message);
                        _promise.Reject();
                    }
                    this.HideMask();
                }));
            });
        }
        #endregion

        #region Action
        /// <summary>
        /// 查看
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnViewClick(object sender, object e)
        {
            if (this.ctlGridViewData.FocusedRowHandle > -1)
            {
                WebRef.StockWarning item = this.ctlGridViewData.GetFocusedRow() as WebRef.StockWarning;
                if (item != null)
                {
                    this.ShowMask("loading...");
                    UIContext.Current.InvokeAsync(() =>
                    {
                        SingleResult<WebRef.VStockThresholdValue> result = new Service.GetService().GetStockThresholdValue(item.WarehouseId, item.MaterialId);
                        this.BeginInvoke(new Action(() =>
                        {
                            if (result.IsSuccess)
                            {
                                if (result.Data == null)
                                {
                                    if (UIContext.Current.ViewManager.ShowQuestion("尚未设置库存预警值,是否现在设置？"))
                                    {
                                        UIContext.Current.ViewManager.PopupView(new StockWarning.SetStockWarningView(),
                                            new PopupViewArgs("设置库存预警"));
                                    }
                                }
                                else
                                {
                                    UIContext.Current.ViewManager.PopupView(new Show.ShowStockWarningView(result.Data), new PopupViewArgs("查看库存预警值"));
                                }
                                _promise.Resolve();
                            }
                            else
                            {
                                UIContext.Current.ViewManager.ShowError(result.Message);
                                _promise.Reject();
                            }
                            this.HideMask();
                        }));
                    });
                }
            }
        }
        /// <summary>
        /// 设置
        /// </summary>
        /// <param name="obj1"></param>
        /// <param name="obj2"></param>
        private void OnSettingClick(object obj1, object obj2)
        {
            UIContext.Current.ViewManager.PopupView(new StockWarning.SetStockWarningView(), new PopupViewArgs("库存预警设置"));
        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="obj1"></param>
        /// <param name="obj2"></param>
        private void OnEditClick(object obj1, object obj2)
        {
            if (this.ctlGridViewData.FocusedRowHandle > -1)
            {
                WebRef.StockWarning item = this.ctlGridViewData.GetFocusedRow() as WebRef.StockWarning;
                if (item != null)
                {
                    this.ShowMask("loading...");
                    UIContext.Current.InvokeAsync(() =>
                    {
                        SingleResult<WebRef.VStockThresholdValue> result = new Service.GetService().GetStockThresholdValue(item.WarehouseId, item.MaterialId);
                        this.BeginInvoke(new Action(() =>
                        {
                            if (result.IsSuccess)
                            {
                                if (result.Data == null)
                                {
                                    if (UIContext.Current.ViewManager.ShowQuestion("尚未设置库存预警值,是否现在设置？"))
                                    {
                                        UIContext.Current.ViewManager.PopupView(new StockWarning.SetStockWarningView(),
                                            new PopupViewArgs("设置库存预警"));
                                    }
                                }
                                else
                                {
                                    UIContext.Current.ViewManager.PopupView(new Edit.UpdateStockWarningView(result.Data),
                                        new PopupViewArgs("修改库存预警"));
                                }
                                _promise.Resolve();
                            }
                            else
                            {
                                UIContext.Current.ViewManager.ShowError(result.Message);
                                _promise.Reject();
                            }
                            this.HideMask();
                        }));
                    });
                }
            }
        }
        /// <summary>
        /// 刷新
        /// </summary>
        /// <param name="obj1"></param>
        /// <param name="obj2"></param>
        private void OnRefreshActionClick(object obj1, object obj2)
        {
            _pageIndex = 0;
            RefreshData();
        }
        private void OnEmbeddedNavigatorButtonClick(object sender, DevExpress.XtraEditors.NavigatorButtonClickEventArgs e)
        {
            if (e.UpdatePageIndex(_pageCount, ref _pageIndex))
            {
                RefreshData();
            }
        }
        /// <summary>
        /// 选择商品&清除所选商品
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ctlBtnSelectMaterial_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            if (e.Button.Tag.Equals("clear"))
            {
                this.ctlBtnSelectMaterial.Text = "";
                itemMaterial = null;
            }
            else
            {
                UIContext.Current.ViewManager.PopupView(new Pickers.MaterialPicker(Domain.Constant.CommonStatus.Enabled),
                    new PopupViewArgs("选择商品") { TagCode = 0x01 });
            }
        }
        #endregion
    }
}
